package org.jeese.common.bean;

import java.io.Serializable;
import java.util.LinkedHashMap;

/**
 * @Description:Json返回结果集工具类
 * @Package:org.jeese.model
 * @author:wubc
 * @version:1.0
 * @date:2017年10月30日-下午1:51:24
 * @Copyright:2017 Inc. All rights reserved.
 */
public class ResponseResult extends LinkedHashMap<String, Object> implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private static final String CODE_KEY = "code";
	private static final String RESULT_KEY = "result";
	private static final String MESSAGE_KEY = "message";
	private static final String DATA_KEY = "data";
	
	private static final String MESSAGE_SUCCESS = "操作成功。";
	private static final String MESSAGE_FAIL = "操作失败。";
	private static final String MESSAGE_ERROR = "操作错误。";
	private static final String MESSAGE_PARAMERROR = "参数错误。";
	private static final String MESSAGE_UNAUTHORIZED = "授权错误。";
	private static final String MESSAGE_UNAUTHENTICATED = "认证错误";
	private static final String MESSAGE_UNLOGIN = "会话超时，请重新登录。";
	private static final String MESSAGE_EXCEPTION = "服务器异常。";
	private static final String MESSAGE_KICKOUT = "当前账户已在其他地方登录，请重新登录。";

	private static final String STATUS_CODE_SUCCESS = "200";
	private static final String STATUS_CODE_FAIL = "201";
	private static final String STATUS_CODE_ERROR = "202";
	private static final String STATUS_CODE_UNLOGIN = "301";
	private static final String STATUS_CODE_KICKOUT = "302";
	private static final String STATUS_CODE_UNPARAM = "400";
	private static final String STATUS_CODE_UNAUTHORIZED = "401";
	private static final String STATUS_CODE_UNAUTHENTICATED = "405";
	private static final String STATUS_CODE_EXCEPTION = "500";


	public ResponseResult() {
		put(CODE_KEY, STATUS_CODE_SUCCESS);
		put(RESULT_KEY, Boolean.TRUE);
		put(MESSAGE_KEY, MESSAGE_SUCCESS);
	}

	/**
	 * 请求成功
	 * 
	 * @return
	 */
	public ResponseResult success() {
		put(CODE_KEY, STATUS_CODE_SUCCESS);
		put(RESULT_KEY, Boolean.TRUE);
		put(MESSAGE_KEY, MESSAGE_SUCCESS);
		return this;
	}

	/**
	 * 请求成功,并带消息
	 * 
	 * @param message
	 * @return
	 */
	public ResponseResult success(String message) {
		put(CODE_KEY, STATUS_CODE_SUCCESS);
		put(RESULT_KEY, Boolean.TRUE);
		put(MESSAGE_KEY, message);
		return this;
	}

	/**
	 * @param obj
	 * @return
	 */
	public ResponseResult successData(Object data) {
		put(CODE_KEY, STATUS_CODE_SUCCESS);
		put(RESULT_KEY, Boolean.TRUE);
		put(MESSAGE_KEY, MESSAGE_SUCCESS);
		put(DATA_KEY, data);
		return this;
	}

	/**
	 * @param message
	 * @param obj
	 * @return
	 */
	public ResponseResult success(String message, Object data) {
		put(CODE_KEY, STATUS_CODE_SUCCESS);
		put(RESULT_KEY, Boolean.TRUE);
		put(MESSAGE_KEY, message);
		put(DATA_KEY, data);
		return this;
	}


	/**
	 * 校验
	 * @param count
	 * @return
	 */
	public ResponseResult validate(int count) {
		if (count > 0) {
			return success();
		} else {
			return error();
		}
	}
	
	/**
	 * 错误
	 * @param message
	 * @return
	 */
	public ResponseResult fail() {
		put(CODE_KEY, STATUS_CODE_FAIL);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, MESSAGE_FAIL);
		return this;
	}

	/**
	 * 错误
	 * 
	 * @param message
	 * @return
	 */
	public ResponseResult fail(String message) {
		put(CODE_KEY, STATUS_CODE_FAIL);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, message);
		return this;
	}



	/**
	 * 错误
	 * @param message
	 * @return
	 */
	public ResponseResult error() {
		put(CODE_KEY, STATUS_CODE_ERROR);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, MESSAGE_ERROR);
		return this;
	}

	/**
	 * 错误
	 * 
	 * @param message
	 * @return
	 */
	public ResponseResult error(String message) {
		put(CODE_KEY, STATUS_CODE_ERROR);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, message);
		return this;
	}


	/**
	 * 参数错误
	 * 
	 * @return
	 */
	public ResponseResult unparam() {
		put(CODE_KEY, STATUS_CODE_UNPARAM);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, MESSAGE_PARAMERROR);
		return this;
	}

	/**
	 * 参数错误
	 * 
	 * @return
	 */
	public ResponseResult unparam(String message) {
		put(CODE_KEY, STATUS_CODE_UNPARAM);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, message);
		return this;
	}

	/**
	 * 出现异常
	 * 
	 * @param e
	 * @return
	 */
	public ResponseResult exception(Exception e) {
		put(CODE_KEY, STATUS_CODE_EXCEPTION);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, MESSAGE_EXCEPTION);
		return this;
	}

	/**
	 * 出现异常
	 * 
	 * @param e
	 * @return
	 */
	public ResponseResult exception(String message) {
		put(CODE_KEY, STATUS_CODE_ERROR);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, message);
		return this;
	}

	/**
	 * 登录失效
	 * @return
	 */
	public ResponseResult unlogin() {
		put(CODE_KEY, STATUS_CODE_UNLOGIN);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, MESSAGE_UNLOGIN);
		return this;
	}

	public ResponseResult kickOut() {
		put(CODE_KEY, STATUS_CODE_KICKOUT);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, MESSAGE_KICKOUT);
		return this;
	}

	/**
	 * 授权错误
	 * @return
	 */
	public ResponseResult unauthorized() {
		put(CODE_KEY, STATUS_CODE_UNAUTHORIZED);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, MESSAGE_UNAUTHORIZED);
		return this;
	}

	/**
	 * 认证错误
	 * @return
	 */
	public ResponseResult unauthenticated() {
		put(CODE_KEY, STATUS_CODE_UNAUTHENTICATED);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, MESSAGE_UNAUTHENTICATED);
		return this;
	}
}
